import json
import os

from faker import Faker


engineering_path = os.path.dirname(os.path.abspath(__file__))
for path, dirs, files in os.walk(engineering_path):
    for i in files:
        if ".log" in i:
            log_path = os.path.join(path, i)
log_path = r"E:\dddownload\Python\step01_语法基础&内置对象\object\信贷系统.log"

api_name = "creditApply", "creditQuery"
for item in api_name:
    with open(log_path, "r", encoding="utf-8") as f:
        content = f.readlines()
        for i in content:
            if "请求参数: " in i and item in i:
                json_data: dict = json.loads(i[i.find("{"):])
                json_data.update({"params": json.loads(json_data.get("params"))})

        with open(f"{item}.json", "w", encoding="utf-8") as fw:
            json.dump(json_data, fw, ensure_ascii=False, indent=2)

# def write_json():
#     json_path = "/Users/mammuthus/PycharmProjects/PythonZeroFoundation/test/creditQuery.json"
#     with open(json_path, "r", encoding="utf-8") as f:
#         json_data = json.load(f)
#         data = {
#             "productCode": f"BPY_{Faker('zh_CN').name()}",
#             "creditNo": f"BPY91_{Faker("zh_CN").ssn()}",
#             "userId": "2406227934",
#             "reqSysCode": "XHY"
#         }
#         json_data.update({"params": data})
#     with open(json_path, "w", encoding="utf-8") as f:
#         json.dump(json_data, f, ensure_ascii=False, indent=4)
#
# if __name__ == '__main__':
#     write_json()
